package com.example.distillapplication.data.repository;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;

import com.example.distillapplication.data.dao.OperationLogDao;
import com.example.distillapplication.data.dao.UserInfoDao;
import com.example.distillapplication.data.database.AppDataBase;
import com.example.distillapplication.data.entity.OperationLog;
import com.example.distillapplication.data.entity.UserInfo;

import java.util.List;

public class DataBaseRepository {
    AppDataBase appDataBase;

    DataBaseListener mDataBaseViewModel;

    private OperationLogDao operationLogDao;
    private UserInfoDao userInfoDao;
    private Thread getAllUserInfoThread;//得到所有用户信息线程
    private Thread getAllOperationLogThread;//得到所有操作信息线程


    private String userInfoAccount;//用户信息Account
    private int userInfoId;//用户信息ID
    private int operationLogId;//操作信息ID
    private Thread getUserInfoByAccountThread;//通过账号得到用户信息线程
    private Thread getOperationLogByIdThread;//通过id得到操作信息线程


    public DataBaseRepository(Context context, DataBaseListener mDataBaseViewModel) {
        appDataBase = AppDataBase.getDataBase(context.getApplicationContext());
        userInfoDao=appDataBase.getUserInfoDao();
        operationLogDao=appDataBase.getOperationLogDao();

        this.mDataBaseViewModel = mDataBaseViewModel;
    }

    public void getAllUserInfo(){
        getAllUserInfoThread = new Thread(new Runnable() {
            @Override
            public void run() {
                mDataBaseViewModel.setAllUserInfo(userInfoDao.getAllUser());
            }
        });
        getAllUserInfoThread.start();
    }

    public void getAllOperationLog(){
        getAllOperationLogThread = new Thread(new Runnable() {
            @Override
            public void run() {
                mDataBaseViewModel.setAllOperationLog(operationLogDao.getAllOperationLog());
            }
        });
        getAllOperationLogThread.start();
    }

    public List<UserInfo> getUserInfoByAccount(String account){
        this.userInfoAccount = account;
        return userInfoDao.findByAccount(userInfoAccount);
    }

    public List<UserInfo> getUserInfoById(int id){
        this.userInfoId = id;
        return userInfoDao.findByUserId(userInfoId);
    }

    public List<OperationLog> getOperationLogByID(int ID){
        this.operationLogId = ID;
        return operationLogDao.findOperationLogById(operationLogId);
    }

    public void insertUserInfo(final UserInfo userInfo){
        Thread insertUserInfoThread = new Thread(new Runnable() {
            @Override
            public void run() {
                userInfoDao.insertUserInfo(userInfo);
            }
        });
        insertUserInfoThread.start();
    }

    public void insertOperationLog(final OperationLog operationLog){
        Thread insertOperationLogThread = new Thread(new Runnable() {
            @Override
            public void run() {
                operationLogDao.insertOperationLog(operationLog);
            }
        });
        insertOperationLogThread.start();
    }
}
